﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml;
using SharpObjects.Configuration;
using SharpObjects.Configuration.ConfigurationProviders;
using SharpObjects.Interfaces;
using SharpObjects.RuleEngine;

namespace SharpRuleEngineTest.RuleElements
{




    public class FieldElement : IInitializable
    {
        public eFieldType FieldType { get; set; }
        public eDataStore FieldDataStore { get; set; }
        public string FieldName { get; set; }

        #region IInitializable Members

        public void Initialize(XmlElement initializationElement)
        {
            XmlConfigurationProvider init = new XmlConfigurationProvider(initializationElement);

            this.FieldType = init.GetEnum<eFieldType>("@type", eFieldType.String);
            this.FieldDataStore = init.GetEnum<eDataStore>("@data_store");
            this.FieldName = init.GetString("self::*");
        }

        #endregion

        public object GetValue(IRuleEngineContext ruleEngineContext, IDataProvider dataProvider)
        {
            object value;

            if (FieldDataStore == eDataStore.Data)
            {
                switch (FieldType)
                {
                    case eFieldType.String:
                        value = dataProvider.GetString(FieldName);
                        break;
                    case eFieldType.Int:
                        value = dataProvider.GetInteger(FieldName);
                        break;
                    case eFieldType.Double:
                        value = dataProvider.GetDouble(FieldName);
                        break;
                    case eFieldType.DateTime:
                        value = dataProvider.GetDateTime(FieldName);
                        break;
                    case eFieldType.Bool:
                        value = dataProvider.GetBoolean(FieldName);
                        break;
                    default:
                        throw new ArgumentException("Unknown eFieldType enum value: " + FieldType);
                }
            }
            else if (FieldDataStore == eDataStore.Context)
            {
                switch (FieldType)
                {
                    case eFieldType.String:
                        value = ruleEngineContext.GetValue<string>(FieldName, string.Empty);
                        break;
                    case eFieldType.Int:
                        value = ruleEngineContext.GetValue<int>(FieldName);
                        break;
                    case eFieldType.Double:
                        value = ruleEngineContext.GetValue<double>(FieldName);
                        break;
                    case eFieldType.DateTime:
                        value = ruleEngineContext.GetValue<double>(FieldName);
                        break;
                    case eFieldType.Bool:
                        value = ruleEngineContext.GetValue<bool>(FieldName);
                        break;
                    default:
                        throw new ArgumentException("Unknown eFieldType enum value: " + FieldType);
                }
            }
            else
            {
                throw new ArgumentException("Unknown eDataStore enum value: " + FieldDataStore);
            }

            return value;
        }
    }
}
